<?php
/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
require_once ('FCBaseGadget.php');


class FCGadgetFactory {
  public static function init($params) {
    // get base uri
    $base_uri = JURI::base();
    // get db instance
    $db = & JFactory::getDBO();
    // this is probably not the best, but there's no other way to do this from a module.
    $install_lock = realpath(dirname(__FILE__)).'/../friendconnect.install';
    if (!file_exists($install_lock)) {
      // create the table to map FC ids
      $query = 'CREATE TABLE `#__friendconnect` '
      . '(`uid` INT(11) NOT NULL ,'
      . '`fcid` VARCHAR(50) NOT NULL,'
      . '`passwd` VARCHAR(50) NOT NULL);';
      $db->setQuery($query);
      $db->query();
      @chmod('modules/mod_friendconnect/', 0777);
      // put the install lock
      file_put_contents($install_lock, date('r'));
    }

    // add required js files
    $document = &JFactory::getDocument();
    $document->addScript('http://www.google.com/jsapi','text/javascript');
    $document->addScriptDeclaration("google.load('friendconnect', '0.8');",'text/javascript');
    $document->addScriptDeclaration("var BASE_URI = '{$base_uri}';",'text/javascript');
    $document->addScript($base_uri.'modules/mod_friendconnect/js/friendconnect.js','text/javascript');

  }
  public static function getGadget($params) {
    self::init($params);
    //print_r($params);
    switch($params->get('gadget_to_render')){
      case 'members':
        require_once ('FCMembersGadget.php');
        $g =  new FCMembersGadget($params);
        break;
      case 'comments':
        require_once ('FCCommentsGadget.php');
        $g =  new FCCommentsGadget($params);
        break;
      case 'social_bar':
        require_once ('FCSocialBarGadget.php');
        $g =  new FCSocialBarGadget($params);
        break;
      case 'polls':
        require_once ('FCPollsGadget.php');
        $g =  new FCPollsGadget($params);
        break;
      case 'newsletter':
        require_once ('FCNewsletterGadget.php');
        $g = new FCNewsletterGadget($params);
        break;
      case 'reviews':
        require_once ('FCReviewsGadget.php');
        $g = new FCReviewsGadget($params);
        break;
      case 'featured_content':
        require_once ('FCFeaturedContentGadget.php');
        $g = new FCFeaturedContentGadget($params);
        break;
      case 'adsense':
        require_once ('FCAdsenseGadget.php');
        $g = new FCAdsenseGadget($params);
        break;
      case 'recommendations':
        require_once ('FCRecommendationsGadget.php');
        $g = new FCRecommendationsGadget($params);
        break;
      case 'recommendation_button':
        require_once ('FCRecommendationButtonGadget.php');
        $g = new FCRecommendationButtonGadget($params);
        break;
      default:
        require_once ('FCSignInGadget.php');
        $g =  new FCSignInGadget($params);
        break;
    }
    $g->render();
  }
}